package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA
 * Description:Collections静态方法学习
 * User:22954
 * Date:2024-02-21
 * Time:21:51
 */
@SuppressWarnings("all")
public class Collections_ {
    /*
    1.Collections类是一个针对集合(Collection)类的工具类,提供了一系列静态方法
    2.主要方法:
    void reverse(List<?> list)               |  void shuffle(List<?> list)
    void sort(List<T> list)                  |  void swap(List<?> list, int i, int j)
    <T>  max(Collection<? extends T> coll)   |  <T> max(Collection<? extends T> coll, Comparator<? super T> comp)
    <T> min(Collection<? extends T> coll)    |  <T> min(Collection<? extends T> coll, Comparator<? super T> comp)
    int frequency(Collection<?> c, Object o) |  void copy(List<? super T> dest, List<? extends T> src)
    boolean replaceAll(List<T> list, T oldVal, T newVal)
     */
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("abc");
        strings.add("kk");
        strings.add("jack");
        strings.add("tom");
        strings.add("hello");
        //1.reserve()逆置
        System.out.println(strings);//[abc, kk, jack, tom, hello]
        Collections.reverse(strings);
        System.out.println(strings);//[hello, tom, jack, kk, abc]
        //2.shuffle()洗牌
        for (int i = 0; i < 3; i++) {
            Collections.shuffle(strings);
            System.out.println(strings);
//            [jack, hello, tom, abc, kk]
//            [kk, hello, jack, tom, abc]
//            [hello, jack, kk, abc, tom]
        }
        //3.sort()排序
        Collections.sort(strings);
        System.out.println(strings);//[abc, hello, jack, kk, tom]
        Collections.sort(strings, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        System.out.println(strings);//[kk, abc, tom, jack, hello]
        //4.swap()交换
        Collections.swap(strings, 1, 3);
//        Collections.swap(strings,10,20);//IndexOutOfBoundsException  i,j的值不能越界
        System.out.println(strings);//[kk, jack, tom, abc, hello]
        //5.max()取最大值
        System.out.println(Collections.max(strings));//tom
        System.out.println(Collections.max(strings, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        }));//hello
        //6.min()取最小值
        System.out.println(Collections.min(strings));//abc
        System.out.println(Collections.min(strings, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        }));//kk
        //7.frequency()出现次数
        System.out.println(Collections.frequency(strings, "tom"));//1
        System.out.println(Collections.frequency(strings, "Tom"));//0
        //8.copy()拷贝
        ArrayList<String> destination = new ArrayList<>();
        /*
        copy()必须保证des.size()>scr.size(),否则会抛出IndexOutOfBoundsException异常
        实例化List时传参改变的是capacity,size仍为0
         */
//        ArrayList<String> destination = new ArrayList<>(5);
        for (int i = 0; i < strings.size(); i++) {
            destination.add("");
        }
        Collections.copy(destination, strings);
        System.out.println(destination);//[kk, jack, tom, abc, hello]
        //9.replaceAll()替换元素
        strings.add("tom");
        System.out.println(Collections.replaceAll(strings, "tom", "汤姆"));//true
        System.out.println(Collections.replaceAll(strings, "Tom", "汤姆"));//false
        System.out.println(strings);//[kk, jack, 汤姆, abc, hello, 汤姆]
    }
}
